#pragma once
#include <iostream>
#include <string>
#include <memory>
#include <fstream>
#include <nlohmann/json.hpp>
#include "dp_common.h"

using json = nlohmann::json;
using namespace std;
//table channel_dp 对应字段
class data_channeldp {
public:
    data_channeldp(){};
    virtual ~data_channeldp(){};
    double get_float_number(json j,string key){
        if(j[key].is_null()) return 0;
        if(j[key].is_string())
        {
            string temp = j[key];
            return atof(temp.c_str());
        }
        return j[key];
    }
    int get_number(json j,string key){
        if(j[key].is_null()) return 0;
        if(j[key].is_string())
        {
            string temp = j[key];
            return atoi(temp.c_str());
        }
        return j[key];
    }
    void initjsonparam(){
        //json解析
        /*
        {"dl":"0","grp":8,"k":1,"b":0,"reg":1007,"cnt":2,"func":3,"addr":1}
        */
        printf("dp json: %s\n",param.c_str());
        json data = json::parse(param);
        if(data.size()!=0)
        {
            m_groupId = get_number(data,"grp");//data["grp"];
            m_jsonparam.slaveId = get_number(data,"addr");//data["addr"];
            m_jsonparam.regStart = get_number(data,"reg");//data["reg"];
            m_jsonparam.regCount = get_number(data,"cnt");//data["cnt"];
            m_jsonparam.fc_code = get_number(data,"func");//data["func"];
            m_value_k=get_float_number(data,"k");//data["k"];
            m_value_b=get_float_number(data,"b");//data["b"];
            m_funcCode =  m_jsonparam.fc_code;
            m_regStart = m_jsonparam.regStart;
            m_slaveId = m_jsonparam.slaveId;
            m_regCount = m_jsonparam.regCount;
        }
    }

public:
    int64_t id;
    int dpid;
    int chid;
    int colid;
    int devid;
    string tag;
    int iotype;
    int vtype;
    int rvtype;
    int bd;
    string exp;
    int rw;
    int upload;
    int enable;
    string param;
    string des;
    dp_param_t m_jsonparam;
    double m_value_k;
    double m_value_b;
    bool  m_bScan;
    int m_groupId;
    int m_funcCode;
    int m_regStart;
    int m_slaveId;
    int m_regCount;
    //json 参数解析
    
};
typedef shared_ptr<data_channeldp> shared_DChanneldpPtr;

//tabel channel 对应字段
class data_channel
{
    public:
    data_channel(){
    }
    virtual ~data_channel(){
    }

public:
    int64_t id;
    int     chid;
    //int     drvid;
    int     drvtype;
    string  drvname;
    int     enable;
    int     interval;
    int     timeout;
    int     trycnts;
    string  param;
    string  des;
};
typedef shared_ptr<data_channel> shared_DChannelPtr;


//table egateway